<?php
/**

 * @package    sketchup

 * @subpackage 

 * @author     Chris Borkowski {@link http://www.guggenheim.org}

 * @author     Created on 28-August-2009

 */
 
// no direct access

defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

class ModelSketchupsJury extends JModel
{
	var $_data = null;
	var $_pagination = null;
	var $_total = null;
	var $_search = null;
	var $_query = null;
	
	function &getData()
	{
		$pagination =& $this->getPagination();
		
		if (empty($this->_data)) {
			$query = $this->buildSearch();
			$this->_data = $this->_getList($query, $pagination->limitstart, $pagination->limit);
		}
		
		return $this->_data;
	}
	
	function buildSearch()
	{
		if (!$this->_query) {
			$search = $this->getSearch();

			$this->_query = "SELECT * FROM #__sketchup";

			if ($search != '') {
				$fields = array('f_name', 'l_name', 'p_title');

				$where = array();

				$search = $this->_db->getEscaped( $search, true );

				foreach ($fields as $field) {
					$where[] = $field . " LIKE '%{$search}%'";
				}

				$this->_query .= ' WHERE ' . implode(' OR ', $where);
				
				
			}
		
		$this->_query .= ' AND jury_select ="1" ';
		
		}

		return $this->_query;
	}
	
	function getTotal()
	{
		if (!$this->_total) {
			$query = $this->buildSearch();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}
	
	function &getPagination() {
		if(!$this->_pagination) {
			jimport('joomla.html.pagination');
			global $mainframe;
			$this->_pagination = new JPagination($this->getTotal(), JRequest::getVar('limitstart', 0), JRequest::getVar('limit', $mainframe->getCfg('list_limit')));
		}

		return $this->_pagination;
	}
	
	function getSearch()
	{
		if (!$this->_search) {
			global $mainframe, $option;

			$search = $mainframe->getUserStateFromRequest( "$option.search", 'search', '', 'string' );
			$this->_search = JString::strtolower($search);
		}

		return $this->_search;
	}
}